package model.viewModels;

import java.util.ArrayList;
import model.Match;
import model.User;
import view.viewPanels.CenterViewPanel;
import control.Communication;

/**
 * model-class for the center-view
 */
public class CenterModel {

    /**
     * hook of the logged in user
     */
    public static User loggedInUser;

    /**
     * related view-class
     */
    private CenterViewPanel centerViewPanel;

    /**
     * List of all actual matches
     */
    private ArrayList<Match> actMatches;
    /**
     * List of all actual matches where it is the logged in user's turn
     */
    private ArrayList<Match> actYourTurnMatches;
    /**
     * List of all archived Matches
     */
    private ArrayList<Match> archMatches;

    /**
     * constructor of the CenterModel class
     * 
     * @param centerView
     *            reference to its view panel
     */
    public CenterModel( CenterViewPanel centerView )
    {
	this.centerViewPanel = centerView;
    }

    /**
     * updates the match lists on login and every 5 minutes
     */
    public final void updateMatchData()
    {
	// clear all lists
	this.actMatches = new ArrayList<Match>();
	this.actYourTurnMatches = new ArrayList<Match>();
	this.archMatches = new ArrayList<Match>();

	// get actMatches from db
	String[] actMatchesData = Communication.getActMatches( loggedInUser.getId(), loggedInUser.getNick() );
	// create actmatches, add to ArrayList
	String sep = "#";
	for ( int i = 0; i < actMatchesData.length; i++ )
	{
	    Match tempMatch = new Match( actMatchesData[i].split( sep ) );
	    this.actMatches.add( tempMatch );

	    // add match to YourTurnMatches when it is my turn
	    if ( tempMatch.getPlayerOne() == loggedInUser )
	    {
		if ( tempMatch.getMoveCount() % 2 == 0 )
		{
		    this.actYourTurnMatches.add( tempMatch );
		}
	    }
	    else
	    {
		if ( tempMatch.getMoveCount() % 2 != 0 )
		{
		    this.actYourTurnMatches.add( tempMatch );
		}
	    }
	}

	// get archMatches from db
	String[] archMatchesData = Communication.getArchMatches( loggedInUser.getId(), loggedInUser.getNick() );
	// create archmatches, add to ArrayList
	for ( int i = 0; i < archMatchesData.length; i++ )
	{
	    this.archMatches.add( new Match( archMatchesData[i].split( sep ) ) );
	}

	// update tables
	this.centerViewPanel.updateTables( this.actMatches, this.archMatches );
    }

    /**
     * updates the CenterView
     */
    public final void updateCenterView()
    {
	this.updateMatchData();
	yourTurnAction();
    }

    /**
     * changes the actGames-table, switches between actMatches and actYourTurnMatches
     */
    public final void yourTurnAction()
    {
	boolean yourTurn = this.centerViewPanel.getCbYourTurn().isSelected();
	if ( yourTurn )
	{
	    this.centerViewPanel.updateTables( this.actYourTurnMatches, null );
	}
	else
	{
	    this.centerViewPanel.updateTables( this.actMatches, null );
	}
    }

    public final CenterViewPanel getCenterViewPanel()
    {
	return this.centerViewPanel;
    }

    public final ArrayList<Match> getActMatches()
    {
	return this.actMatches;
    }

    public final ArrayList<Match> getActYourTurnMatches()
    {
	return this.actYourTurnMatches;
    }

    public final ArrayList<Match> getArchMatches()
    {
	return this.archMatches;
    }

}
